package C038;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: C03
 * Package: C038
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/29 21:30
 * @Version 1.0
 * @Since 1.0
 */
public class C03 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println(permute(arr));
    }


    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        process(nums, 0, res);
        return res;
    }

    private static void process(int[] nums, int path, List<List<Integer>> res) {
        if(path == nums.length) {
            List<Integer> ans = new ArrayList<>();
            for (int i = 0; i < nums.length; i++) {
                ans.add(nums[i]);
            }
            res.add(ans);
            return;
        }
        // 交换位置进行枚举
        for (int i = path; i < nums.length; i++) {
            swap(nums, path, i);
            process(nums, path + 1, res);
            swap(nums, path, i);
        }
    }

    private static void swap(int[] nums, int path, int i) {
        int temp = nums[path];
        nums[path] = nums[i];
        nums[i] = temp;
    }
}
